<template>
    <a-modal
        title="物料选择"
        class="large-modal"
        :visible="visible"
        :centered="true"
        @ok="handleOk"
        @cancel="visible = false"
    >
        <searchData :searchData="realSearchArr" @search="search"></searchData>
        <b-table
            ref="table"
            :request="loadData"
            :selectConfig="{use:true,type:'radio',rowKey}"
            :columns="columns === null ? localColumns : columns"
            @row-click="rowClick">
            <span slot="num" slot-scope="text, record, index">{{ index + 1 }}</span>
        </b-table>
    </a-modal>
</template>
<script>
import { goodsSearchList } from '@/api/material/goods/goods'
import { getOrgUsers } from '@/api/commons'

export default {
    name: 'detail-dialog',
    props: {
        urlFun: {
            type: Function
        },
        params: {
            type: Object,
            default() {
                return {}
            }
        },
        otherData: {
            type: Object,
            default() {
                return {}
            }
        },
        rowKey: {
            type: String,
            default: 'id'
        },
        columns: {
            type: Array,
            default: null
        },
        searchArr: {
            type: Array,
            default: null
        }
    },
    data: function() {
        return {
            visible: false,
            localColumns: [
                {
                    title: '序号',
                    scopedSlots: { customRender: 'num' },
                    width: 70
                },
                {
                    title: '物料名称',
                    dataIndex: 'name',
                    width: 120
                },
                {
                    title: '物料编码',
                    dataIndex: 'code',
                    width: 140
                },
                {
                    title: '规格型号',
                    dataIndex: 'specificationModel'
                },
                {
                    title: '单位',
                    dataIndex: 'unit',
                    width: 100
                },
                {
                    title: '物资目录名称',
                    dataIndex: 'catalogName',
                    width: 140
                },
                {
                    title: '物资目录编码',
                    dataIndex: 'catalogCode',
                    width: 120
                }, {
                    title: '状态',
                    dataIndex: 'commonStatusStr',
                    width: 70
                }
            ],
            localSearchArr: [
                {
                    name: '物料名称',
                    value: 'name',
                    type: 'input'
                },
                {
                    name: '物料编码',
                    value: 'code',
                    type: 'input'
                },
                {
                    name: '物资目录名称',
                    value: 'catalogName',
                    type: 'input'
                },
                {
                    name: '物资目录编码',
                    value: 'catalogCode',
                    type: 'input'
                }
            ],
            selectRow: null,
            queryParams: {},
            loadData: params => {
                let searchData = { commonStatus: 1 }
                searchData = Object.assign(searchData, params, this.otherData, this.params, this.queryParams)
                let result = this.urlFun || goodsSearchList
                return result(searchData)
            }
        }
    },
    computed: {
        realSearchArr() {
            const arr = this.searchArr === null ? this.localSearchArr : this.searchArr
            arr.push({
                name: '规格',
                value: 'specificationModel',
                type: 'input'
            })
            return arr
        }
    },
    methods: {
        open() {
            this.visible = true
            this.search()
        },
        rowClick(e, row) {
            this.selectRow = row
        },
        handleOk() {
            if (this.selectRow === null) {
                return this.$noticeWarning('请选择数据')
            }
            this.visible = false
            this.$emit('createList', this.selectRow)
        },
        search(val) {
            this.queryParams = val || {}
            this.$refs.table && this.$refs.table.getData(null, true)
        }
    }
}
</script>
